<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="负责人" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择负责人"
          style="width: 100%"
          filterable
          clearable
        >
          <el-option
            v-for="user in userList"
            :key="user.userId"
            :label="user.userName"
            :value="user.userId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品厂家" prop="productFactory">
        <el-input
          v-model="queryParams.productFactory"
          placeholder="请输入产品厂家"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="市场价" prop="marketPrice">
        <el-input
          v-model="queryParams.marketPrice"
          placeholder="请输入市场价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="出货价" prop="shippingPrice">
        <el-input
          v-model="queryParams.shippingPrice"
          placeholder="请输入出货价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:product:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crm:product:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:product:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crm:product:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <div>
      <div style="margin-top: 15px" class="body-box" v-loading="loading">
        <div class="waterfall-layout">
          <div
            v-for="product in productList"
            :key="product.productId"
            class="customer-card"
          >
            <div class="card">
              <div class="header">
                <h2 class="titel">
                  产品名称: {{ product.productName }}
                  <el-dropdown
                    trigger="click"
                    @click.native.stop
                    style="float: right; font-size: 1.2em; color: #0277bd"
                  >
                    <label><i class="el-icon-s-operation"></i></label>
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item
                        icon="el-icon-edit"
                        @click.native.stop="handleUpdate(product)"
                        >修改</el-dropdown-item
                      >
                      <el-dropdown-item
                        icon="el-icon-delete"
                        @click.native.stop="handleDelete(product)"
                        >删除</el-dropdown-item
                      >
                    </el-dropdown-menu>
                  </el-dropdown>
                </h2>
                <div class="details">
                  <p>产品厂家: {{ product.productFactory }}</p>
                  <p>市场价: {{ product.marketPrice }}</p>
                  <p>出货价: {{ product.shippingPrice }}</p>
                </div>
                <div class="total-amount">
                  <p>负责人: {{ product.userName }}</p>
                  <!-- <p>
                    产品文件:
                    <a :href="product.productFile" target="_blank">点击查看</a>
                  </p> -->
                  <p>
                    功能明细:
                    <a :href="product.productFunction" target="_blank"
                      >点击下载</a
                    >
                  </p>
                </div>
              </div>

              <div class="opportunity-category">
                <h3>成交客户</h3>
                <ul class="opportunity-list">
                  <li v-for="item in product.bos" :key="item.customerName">
                    <span
                      >客户: {{ item.customerName }}--成交时间:
                      {{ item.makeDate }}--成交金额: {{ item.makeMoney }}</span
                    >
                  </li>
                </ul>
              </div>
            </div>
          </div>
        </div>
        <!-- </el-card> -->
      </div>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <!-- <el-empty :image-size="450" v-else></el-empty> -->
    <!-- 添加或修改产品列表对话框 -->
    <el-drawer
      :title="title"
      direction="rtl"
      :visible.sync="visible"
      size="90%"
      append-to-body
      :wrapperClosable="false"
    >
      <el-card
        class="box-card"
        style="width: 96%; margin-left: 20px; height: 90vh"
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="产品名称" prop="productName">
                <el-input
                  v-model="form.productName"
                  placeholder="请输入产品名称"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="产品厂家" prop="productFactory">
                <el-input
                  v-model="form.productFactory"
                  placeholder="请输入产品厂家"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="市场价" prop="marketPrice">
                <el-input
                  v-model="form.marketPrice"
                  placeholder="请输入市场价"
                  type="number"
                >
                  <template slot="append">{{
                    digitUppercase(form.marketPrice)
                  }}</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="出货价" prop="shippingPrice">
                <el-input
                  v-model="form.shippingPrice"
                  placeholder="请输入出货价"
                  type="number"
                >
                  <template slot="append">{{
                    digitUppercase(form.shippingPrice)
                  }}</template>
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="负责人" prop="userId">
                <el-select
                  v-model="form.userId"
                  filterable
                  clearable
                  placeholder="请选择分享人列表"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in userList"
                    :key="item.userId"
                    :label="item.userName"
                    :value="item.userId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="产品简介" prop="productScheme">
                <el-input
                  v-model="form.productScheme"
                  type="textarea"
                  placeholder="请输入内容"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="产品功能" prop="productFunction">
                <file-upload
                  v-model="form.productFunction"
                  :fileType="['docx', 'doc', 'xlsx', 'xls', 'pdf']"
                />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
              <el-form-item label="产品文件" prop="productFile">
                <file-upload v-model="form.productFile" :fileType="['pdf']" />
              </el-form-item>
            </el-col> -->
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
              </el-form-item>
            </el-col></el-row
          >
        </el-form>
      </el-card>
    </el-drawer>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
} from "@/api/crm/product";

import { getUserListByStatus } from "@/api/system/user";
export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品列表表格数据
      productList: [],
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        productName: null,
        productFactory: null,
        marketPrice: null,
        shippingPrice: null,
        productFunction: null,
        productFile: null,
        productScheme: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "请选择负责人", trigger: "change" },
        ],
        productName: [
          { required: true, message: "请输入产品名称", trigger: "blur" },
        ],
        productFactory: [
          { required: true, message: "请输入产品厂家", trigger: "blur" },
        ],
        marketPrice: [
          { required: true, message: "请输入市场价", trigger: "blur" },
        ],
        shippingPrice: [
          { required: true, message: "请输入出货价", trigger: "blur" },
        ],
        productScheme: [
          { required: true, message: "请上传产品功能", trigger: "change" },
        ],
        productFunction: [
          { required: true, message: "请上传产品文件", trigger: "change" },
        ],
        productFile: [
          { required: true, message: "请输入产品简介", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getUserListByStatus();
  },
  methods: {
    getUserListByStatus() {
      getUserListByStatus({ status: "0" }).then((res) => {
        this.userList = res.data;
      });
    },
    /** 查询产品列表列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.visible = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        productId: null,
        productName: null,
        productFactory: null,
        marketPrice: null,
        shippingPrice: null,
        productFunction: null,
        productFile: null,
        productScheme: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.productId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.visible = true;
      this.title = "添加产品列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.visible = true;
        this.title = "修改产品列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.visible = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.visible = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal
        .confirm('是否确认删除产品列表编号为"' + productIds + '"的数据项？')
        .then(function () {
          return delProduct(productIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "crm/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style scoped>
.waterfall-layout {
  column-count: 2;
  column-gap: 15px;
}

.customer-card {
  margin-bottom: 15px;
  break-inside: avoid;
}

.body-box {
  max-height: 72vh;
  min-height: 72vh;
  overflow: auto;
}

.total-amount {
  display: flex;
  margin-top: 10px;
  font-weight: bold;
  color: black;
  justify-content: space-between;
}

a:hover {
  color: blue;
}
</style>